package com.qcu.controller.staff;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qcu.annotation.LimitRate;
import com.qcu.annotation.RequireAuthorization;
import com.qcu.entity.Archive;
import com.qcu.exception.RepeatException;
import com.qcu.service.ArchiveService;
import com.qcu.util.Response;
import com.qcu.util.ResponseCode;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Name: MyComputer
 * @ClassName ArchiveController
 * @Description:
 * @date 2025/2/7 12:57
 */

@RestController("staffArchiveController")
@RequestMapping("/staff/archive")
public class ArchiveController {

    @Resource
    private ArchiveService archiveService;

    /**
     * 创建档案
     * TODO: 判断档案是否存在
     * @param archive
     * @return
     */
    @PostMapping("/create")
    @RequireAuthorization({"STAFF","PLAIN"})
    public Response create(@RequestBody Archive archive) {
        try {
            boolean saved = archiveService.saveArchive(archive);
            return saved ? Response.success(archive) : Response.failure(ResponseCode.FAILURE, "创建档案失败");
        } catch (Exception e) {
            throw new RepeatException(e.getMessage());
        }
    }

    /**
     * 获取所有档案
     * @return
     */
    @GetMapping("/all")

    @LimitRate(key = "getAllArchiveOfUserHStaff",permitsPerSecond = 2,expire = 10,msg = "八嘎雅鹿，你滴点那么快滴干什么滴干活...")
    @RequireAuthorization({"STAFF","PLAIN"})
    public Response getAll() {
        List<Archive> archives = archiveService.list();
        return Response.success(archives);
    }

    /**
     * 根据 ID 获取档案
     * @param inhabitantID
     * @return
     */
    @GetMapping("/get/{inhabitantID}")
    @LimitRate(key = "getUserArchiveByInhabitantId",permitsPerSecond = 2,expire = 10,msg = "八嘎雅鹿，你滴点那么快滴干什么滴干活...")
    @RequireAuthorization({"STAFF","PLAIN"})
    public Response get(@PathVariable String inhabitantID) {
        LambdaQueryWrapper<Archive> archiveLambdaQueryWrapper = new LambdaQueryWrapper<>();
        archiveLambdaQueryWrapper.eq(Archive::getInhabitantId, inhabitantID);
        Archive archive = archiveService.getOne(archiveLambdaQueryWrapper);
        return archive != null ? Response.success(archive) : Response.failure(ResponseCode.NOT_FOUND, "档案未找到");
    }

    /**
     * 更新档案
     * @param archive
     * @return
     */
    @PutMapping("/update")
    @RequireAuthorization({"STAFF","PLAIN"})
    public Response update(@RequestBody Archive archive) {
        boolean updated = archiveService.updateByIdAndToLog(archive);
        return updated ? Response.success(archive) : Response.failure(ResponseCode.FAILURE, "更新档案失败");
    }

    /**
     * 删除档案
     * @param archiveId
     * @return
     */
    @DeleteMapping("/delete/{archiveId}")
    @RequireAuthorization({"STAFF","PLAIN"})
    public Response delete(@PathVariable Long archiveId) {
        boolean deleted = archiveService.removeById(archiveId);
        return deleted ? Response.success("删除成功") : Response.failure(ResponseCode.FAILURE, "删除档案失败");
    }

}
